﻿//-----------------------------------------------------------------------
// <copyright file="XmlInterfaceHelper.cs" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>
//----------------------------------------------------------------------- 

namespace ChatBox.DomainObjects.XmlFeedObjects
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Xml.Linq;

    using ChatBox.Utility;

    /// <summary>
    /// Helper methods on XmlInterface and lists of them
    /// </summary>
    public static class XmlInterfaceHelper
    {
        /// <summary>
        /// read e2 interfaces from xml
        /// </summary>
        /// <param name="doc">the xml document</param>
        /// <returns>the contructed object list</returns>
        public static IEnumerable<XmlInterface> ToInterfaces(this XContainer doc)
        {
            return 
               from intf in doc.Descendants("xmlexport")
               select new XmlInterface
                   {
                       InterfaceName = intf.Attribute("iface").Value,
                       NodeId = intf.Attribute("node_id").Value.ToInteger(),
                       Description = intf.Value
                   };
        }

        /// <summary>
        /// Find an interface in the list by name
        /// </summary>
        /// <param name="interfaces">the list of interfaces</param>
        /// <param name="name">the name to find</param>
        /// <returns>the itnerface found</returns>
        public static XmlInterface FindByName(this IEnumerable<XmlInterface> interfaces, string name)
        {
            foreach (var xmlInterface in interfaces)
            {
                if (String.Equals(xmlInterface.InterfaceName, name, StringComparison.OrdinalIgnoreCase))
                {
                    return xmlInterface;
                }
            }

            return null;
        }
    }
}
